/*
 * @Author: your name
 * @Date: 2021-01-04 09:50:30
 * @LastEditTime: 2021-02-20 16:23:00
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \yellow-member-admin\src\router\index.js
 */
import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const router = new Router({
    routes: [{
        path: '/',
        redirect: '/dashboard'
    },
    {
        path: '/',
        component: () =>
            import( /* webpackChunkName: "home" */ '../layout/Home.vue'),
        meta: { title: '自述文件' },
        children: [{
            path: '/dashboard',
            component: () =>
                import( /* webpackChunkName: "dashboard" */ '@/page/home'),
            meta: { title: '系统首页' }
        },
        {
            path: '/img-list',
            component: () =>
                import( /* webpackChunkName: "dashboard" */ '@/page/img-list'),
            meta: { title: '图片列表' }
        },
        {
            path: '/add-img',
            component: () =>
                import( /* webpackChunkName: "dashboard" */ '@/page/add-img'),
            meta: { title: '添加图片' }
        },
        {
            path: '/404',
            component: () =>
                import( /* webpackChunkName: "404" */ '@/page/404.vue'),
            meta: { title: '404' }
        },
        {
            path: '/403',
            component: () =>
                import( /* webpackChunkName: "403" */ '@/page/403.vue'),
            meta: { title: '403' }
        }
        ]
    },
    {
        path: '/login',
        component: () =>
            import( /* webpackChunkName: "login" */ '@/page/login'),
        meta: { title: '登录' }
    },
    {
        path: '*',
        redirect: '/404'
    }
    ]
});
// 导航守卫
// 判断用户是否登录，添加前置导航守卫
router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} `;
    const token = localStorage.getItem('token');
    if (token) {
        next()
    } else {
        if (to.path === '/login') { // 如果是登录页
            next();
        } else {
            next({ path: '/login' })
        }
    }
})
export default router